'use strict';

angular.module('myApp.microphone', ['ngRoute'])

    .config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/testing/microphone', {
            templateUrl: 'pages/testing/microphone/microphone.html',
            // template: '<video src=""></video>',
            controller: 'MicrophoneCtrl'
        });
    }])

    .controller('MicrophoneCtrl', function ($rootScope, $scope, $location) {
        $rootScope.setStatusBar(true)

        const items = [{
            title: '音量检测',
            sensorId: 1
        }, {
            title: '时域波形',
            sensorId: 2
        }, {
            title: '频域波形',
            sensorId: 3
        },
        ]
        $rootScope.items = items
        $rootScope.rowNum = 1
        $rootScope.colNum = 1
        // let audioContext = new (window.AudioContext || window.webkitAudioContext)()
        var wave;
        var rec = Recorder({
            onProcess: function (buffers, powerLevel, bufferDuration, bufferSampleRate) {
                wave.input(buffers[buffers.length - 1], powerLevel, bufferSampleRate);//输入音频数据，更新显示波形
            }
        });

        $rootScope.localCallback[$location.path()] = (index) => {
            $rootScope.title = items[index].title
            try {
                rec._stop()
                rec.close()
            } catch (error) {
                console.log(error)
            }

            rec.open(() => {
                if (index == 0) {
                    wave = Recorder.WaveView({ elem: "#myChart" }); //创建wave对象
                } else if (index == 1) {
                    wave = Recorder.WaveSurferView({ elem: "#myChart", direction: -1 }); //创建wave对象
                } else {
                    wave = Recorder.FrequencyHistogramView({ elem: "#myChart", lineCount: 60, fps: 10, fallDuration: 500, stripeFallDuration: 1000, scale: 1 }); //创建wave对象
                }
                rec.start();
            });
        }

        $scope.$on('$routeChangeStart', ($event, next, current) => {
            console.log(rec)
            rec._stop()
            rec.close()
            // audioContext.close()
            // audioContext = rec.Ctx
        });

        navigator.mediaDevices.enumerateDevices().then(devices => {
            const audioDevices = devices.filter(device => device.kind == 'audioinput')
            // console.log(audioDevices)
            // if (audioDevices.length == 2) {
            //     window.audio_constraints = { audio: { deviceId: { exact: audioDevices[1].deviceId } } }
            // }
            $rootScope.localMenus[$location.path()] = audioDevices.map((device, index) => {
                return {
                    text: device.label || '麦克风 ' + index,
                    callback: () => {
                        if ($scope.currentStream) {
                            $scope.currentStream.getTracks().forEach(track => {
                                track.stop();
                            });
                        }
                        const constraints = { audio: { deviceId: { exact: device.deviceId } } };
                        console.log(constraints)
                        window.audio_constraints = constraints
                        // navigator.mediaDevices.getUserMedia(constraints).then(function (mediaStream) {
                        //     $scope.currentStream = mediaStream

                        //     console.log('getUserMedia:', mediaStream)
                        //     audioContext.resume().then(() => {
                        //         audioContext.createMediaStreamSource(mediaStream).connect(scriptProcessor)
                        //         updateData()
                        //     })
                        //     console.log(scriptProcessor)

                        // }).catch(function (err) {
                        //     console.log(err.name + ": " + err.message);
                        // });
                    }
                }
            })
            $rootScope.updatePageInfo()

        });
    });